﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using WmsApp.Service;
using WmsApp.WmsService;

namespace WmsApp.Window
{
    public partial class FormInvoiceBillInStoreBillDtlSelector : BaseForm
    {
        private string providerCode = string.Empty;
        private const string ConditionInStoreBill =" and ProviderCode ='{0}' and AmountTax> InStoreBillAmount ";
        private string inStoreBillDtlID = string.Empty;
        private InStoreBillDtl dtl = null;

        public InStoreBillDtl FInStoreBillDtl
        {
            get { return dtl; }
        }

        private List<InvoiceBillInStoreBillDtl> invoiceBillInStoreBillDtls = new List<InvoiceBillInStoreBillDtl>();


        public FormInvoiceBillInStoreBillDtlSelector(string inStoreBillDtlID, string providerCode)
        {
            this.inStoreBillDtlID = inStoreBillDtlID;
            this.providerCode = providerCode;
            InitializeComponent();
            this.repositoryItemButtonEditAddToDtl.ButtonClick +=
                (o, args) =>
                    {
                        var invoiceBill = (InvoiceBill) this.gridViewDown.GetRow(this.gridViewDown.FocusedRowHandle);
                        AddInvoiceToDtl(invoiceBill);
                    };
            this.repositoryItemButtonEditCancelInvoice.ButtonClick +=
                (o, args) =>
                    {
                        var invoiceBillInStoreBillDtl = (InvoiceBillInStoreBillDtl)this.gridViewUp.GetRow(this.gridViewUp.FocusedRowHandle);
                        CancelInvoiceFromDtl(invoiceBillInStoreBillDtl);
                    };
            this.LoadInStoreBillDtl(this.inStoreBillDtlID);
            this.LoadInvoiceBillInStoreBillDtls(this.inStoreBillDtlID);
            this.LoadCanBindingInvoiceList(this.dtl.AmountTax - (this.dtl.InvoiceDtlTotalAmountTax ?? 0));
            ShowDataInControl();
        }

        private void ShowDataInControl()
        {
            this.textEditBillID.Text = this.dtl.BillID;
            this.textEditAmountTax.Text = this.dtl.AmountTax.ToString();
            decimal remainAmountTax = dtl.AmountTax - (dtl.InvoiceDtlTotalAmountTax ?? 0);
            this.textEditMountTaxRemain.Text = remainAmountTax.ToString();
            this.textEditNewGoodsCode.Text = this.dtl.NewGoodsCode;
            this.textEditNewGoodsName.Text = this.dtl.NewGoodsName;
        }

        private void AddInvoiceToDtl(InvoiceBill invoiceBill)
        {
            if(invoiceBill.PayAmountTax == 0)
            {
                Exceptions.ExceptionHandler.ShowException("分摊金额不能为0！");
                return;
            }

            var q = from ib in invoiceBillInStoreBillDtls
                    where ib.InvoiceNo == invoiceBill.InvoiceNo
                    select ib;
            if(q.Count() >0)
            {
                Exceptions.ExceptionHandler.ShowException("入库明细中已经绑定改发票，不能再次添加，如要添加，先取消该发票再添加。");
                return;
            }

            var invoiceBillInStoreBillDtl = InvoiceBillInStoreBillDtl.CreateInvoiceBillInStoreBillDtl(invoiceBill.InvoiceNo,
                                                                              this.inStoreBillDtlID);
            invoiceBillInStoreBillDtl.InvoiceDtlAmountTax = invoiceBill.PayAmountTax;
            var client = Factory.WmsServiceFactory.GetServiceClient();
            try
            {
                client.AddInvoiceBillInStoreBillDtl(
                            new List<InvoiceBillInStoreBillDtl>()
                                                                {
                                                                    invoiceBillInStoreBillDtl
                                                                });
                this.LoadInStoreBillDtl(this.inStoreBillDtlID);
                this.LoadInvoiceBillInStoreBillDtls(this.inStoreBillDtlID);
                this.LoadCanBindingInvoiceList(this.dtl.AmountTax - (this.dtl.InvoiceDtlTotalAmountTax??0));
                ShowDataInControl();
            }
            catch (Exception exception)
            {
                Exceptions.ExceptionHandler.ShowException(exception);
            }

        }

        private void CancelInvoiceFromDtl(InvoiceBillInStoreBillDtl invoiceBillInStoreBillDtl)
        {
            var client = Factory.WmsServiceFactory.GetServiceClient();
            try
            {
                client.CancelInStoreBillDtlFromInvoiceBill(invoiceBillInStoreBillDtl.InvoiceNo, invoiceBillInStoreBillDtl.InStoreBillDtlID);

                this.LoadInStoreBillDtl(this.inStoreBillDtlID);
                this.LoadInvoiceBillInStoreBillDtls(this.inStoreBillDtlID);
                this.LoadCanBindingInvoiceList(this.dtl.AmountTax - (this.dtl.InvoiceDtlTotalAmountTax ?? 0));
                ShowDataInControl();
            }
            catch (Exception exception)
            {
                Exceptions.ExceptionHandler.ShowException(exception);
            }
        }

        private void LoadInvoiceBillInStoreBillDtls(string dtlID)
        {
            var client = Factory.WmsServiceFactory.GetServiceClient();
            string condition =String.Format( " and InStoreBillDtlID = '{0}'",dtlID);
            invoiceBillInStoreBillDtls = client.GetInvoiceBillInStoreBillDtls(condition);
            this.gridControlUp.DataSource = invoiceBillInStoreBillDtls;
        }

        private void LoadInStoreBillDtl(string dtlID)
        {
            var client  =Factory.WmsServiceFactory.GetServiceClient();
            dtl = client.GetInStoreBillDtl(dtlID);
        }

        /// <summary>
        /// 加载发票
        /// </summary>
        /// <param name="payAmountTax">本次需要分摊金额</param>
        private void LoadCanBindingInvoiceList(decimal payAmountTax)
        {
            var service = EntityServiceFactory.GetEntityService<InvoiceBill, InvoiceBillService>();
            string condition = String.Format(ConditionInStoreBill, providerCode);
            var invoiceBills = service.GetEntityList(condition);
            foreach (var invoiceBill in invoiceBills)
            {
                //如果发票金额-已经分摊金额>=本入库明细需要金额
                if ((invoiceBill.AmountTax - invoiceBill.InStoreBillAmount) == payAmountTax)
                {
                      invoiceBill.PayAmountTax = payAmountTax;
                }
                else
                {
                    invoiceBill.PayAmountTax = (invoiceBill.AmountTax - invoiceBill.InStoreBillAmount);
                }
              
            }
            this.gridControlDown.DataSource = invoiceBills;
        }

        private void CmdRefresh_Click(object sender, EventArgs e)
        {
            this.LoadInStoreBillDtl(this.inStoreBillDtlID);
            this.LoadInvoiceBillInStoreBillDtls(this.inStoreBillDtlID);
            this.LoadCanBindingInvoiceList(this.dtl.AmountTax - (this.dtl.InvoiceDtlTotalAmountTax ?? 0));
        }
    }
}